package com.zhku.shoppingspringboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhku.shoppingspringboot.common.R;
import com.zhku.shoppingspringboot.pojo.entity.Categories;
import com.zhku.shoppingspringboot.pojo.entity.Goods;
import com.zhku.shoppingspringboot.pojo.vo.CategoryVO;
import com.zhku.shoppingspringboot.pojo.vo.GoodsVO;
import com.zhku.shoppingspringboot.service.ICategoriesService;
import com.zhku.shoppingspringboot.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jieeee
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    private ICategoriesService categoriesService;
    @Autowired
    private IGoodsService goodsService;
    @GetMapping("/head")
@Cacheable(value = "shopping",key = "'categories_head'")
    public R<List<CategoryVO>> getAllCategory(){
    //获取列表数据
    List<CategoryVO> categoryVOList=   categoriesService.getAllCategory();
    //遍历获取某个分类下的商品数据
    for (int i = 0; i < categoryVOList.size(); i++) {
        CategoryVO categoryVO = categoryVOList.get(i);
        List<GoodsVO> goods= goodsService.getGoodsByCategoryId(categoryVO.getId());
        categoryVO.setGoods(goods);
        categoryVOList.set(i,categoryVO);
    }


    return R.success(categoryVOList);
    }


    @GetMapping()
    @Cacheable(value = "shopping",key = "'categories_Secondary'")
    public R<CategoryVO> getSecondaryCategory(@RequestParam Integer id){
        CategoryVO categories = categoriesService.getSecondaryCategory(id);
        List<CategoryVO> children = categories.getChildren();
        for (int i = 0; i < children.size(); i++) {
            CategoryVO categoryVO = children.get(i);
            List<GoodsVO> goodsByCategoryId = goodsService.getGoodsByCategoryId(categoryVO.getId());
            children.set(i,categoryVO.setGoods(goodsByCategoryId));
        }
        categories.setChildren(children);
return R.success(categories);

    }



}
